﻿using System;
using System.Collections;
using System.Collections.Generic;
using Castle.Services.Transaction;
using Meqs.Entity;
using Meqs.Entity.Views;
using Meqs.Logic.Interface;

namespace Meqs.Logic.Implement
{
    [Transactional]
    public class SheetItemsLogic : CommonLogic<SheetItems, int>, ISheetItemsLogic
    {
        #region Implementation of ICommonLogic<SheetItems, int>

        /// <sammury>
        /// 创建对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        [Transaction(TransactionMode.RequiresNew)]
        public int Create(SheetItems entity)
        {
            return CreateObject(entity);
        }

        /// <sammury>
        /// 更新对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        [Transaction(TransactionMode.RequiresNew)]
        public int Update(SheetItems entity)
        {
            return UpdateObject(entity);
        }

        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="entityId"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        [Transaction(TransactionMode.RequiresNew)]
        public int Delete(int entityId, string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("RID", entityId);
            ht.Add("SchoolMark", schoolMark);
            return DeleteCustomer("SheetItems.Delete", ht);
        }

        /// <summary>
        /// 删除所有对象
        /// </summary>
        /// <returns></returns>
        [Transaction(TransactionMode.RequiresNew)]
        public int DeleteAll(string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            return DeleteCustomer("SheetItems.Delete", ht);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entityId"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public SheetItems FindById(int entityId, string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("RID", entityId);
            ht.Add("SchoolMark", schoolMark);
            return FindCustomerByID<SheetItems>("SheetItems.FindById", ht);
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public int FindCount(string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            return FindCustomerByCount("SheetItems.FindCount", ht);
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public IList<SheetItems> FindAll(string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            return FindCustomer<SheetItems>("SheetItems.Find", ht);
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <returns></returns>
        public int FindCount(Hashtable table)
        {
            return FindObjectByCount(table);
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        public IList<SheetItems> FindAll(Hashtable table)
        {
            return FindObject(table);
        }

        #endregion

        /// <summary>
        /// 创建SheetItems
        /// </summary>
        /// <param name="items"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        [Transaction(TransactionMode.RequiresNew)]
        public int Create(SheetItems items, string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SheetItems", items);
            ht.Add("SchoolMark", schoolMark);
            return (int)InsertCustomer("SheetItems.InsertSheetItems", ht);
        }

        /// <summary>
        /// 创建SheetItems
        /// </summary>
        /// <param name="items"></param>
        /// <param name="schoolMark"></param>
        /// <param name="teacherId"></param>
        /// <param name="currentStudentEval"></param>
        /// <returns></returns>
        [Transaction(TransactionMode.RequiresNew)]
        public int Create(SheetItems items, string schoolMark, int teacherId, StudentEvals currentStudentEval)
        {
            Hashtable ht = new Hashtable();
            if (items.EsID == 0)
            {
                TeacherInfo ti = FindCustomerByID<TeacherInfo>("TeacherInfo.FindById", teacherId);
                String esName = String.Format("{0}({1})-{2}", ti.TeachName, ti.TeachCode,
                                              DateTime.Now.ToString("yyMMdd"));

                EvalSheets es = new EvalSheets(0, teacherId, currentStudentEval.TemplateID, esName, DateTime.Now, 0, 0,
                                               1, currentStudentEval.RID, 0);

                ht.Add("SchoolMark", schoolMark);
                ht.Add("EvalSheets", es);
                int esId = (int)InsertCustomer("EvalSheets.CreateStudentEval", ht);
                
                items.EsID = esId;
            }
            ht.Clear();
            ht.Add("SheetItems", items);
            ht.Add("SchoolMark", schoolMark);
            return (int)InsertCustomer("SheetItems.InsertSheetItems", ht);
        }

        /// <summary>
        /// 创建SheetItems
        /// </summary>
        /// <param name="items"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        [Transaction(TransactionMode.RequiresNew)]
        public int Update(SheetItems items, string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SheetItems", items);
            ht.Add("SchoolMark", schoolMark);
            return UpdateCustomer("SheetItems.UpdateSheetItems", ht);
        }

        /// <summary>
        /// 查询检查单明细
        /// </summary>
        /// <param name="evalsheetID"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public IList<SheetItemsExtend> FindAllByEvalItemsID(int evalsheetID, string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("EsID", evalsheetID);
            ht.Add("SchoolMark", schoolMark);
            return FindCustomer<SheetItemsExtend>("SheetItems.FindAllByEvalItemsID", ht);
        }
    }
}
